using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Reflection;

namespace Camalon.Drawing
{
	public class WarpConverter:ExpandableInstanceConverter
	{

		public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
		{
			if ( value is string )
			{
				
				string sVal = (string)value;
				if( sVal.Length == 0) return Warp.Empty;
				if( sVal.ToLower().IndexOf("null") != -1) return null;
				if( sVal.ToLower().IndexOf("empty") != -1) return Warp.Empty;

				string[] args	= sVal.Split(';');
				string   tl		= args[0];
				string[] tla	= tl.Split(',');
				Point	 ptl	= new Point( int.Parse(tla[0]), int.Parse(tla[1]));    
				string   tr		= args[1];
				string[] tra	= tr.Split(',');
				Point	 ptr	= new Point( int.Parse(tra[0]), int.Parse(tra[1]));    
				string	 bl		= args[2];
				string[] bla	= bl.Split(',');
				Point	 pbl	= new Point( int.Parse(bla[0]), int.Parse(bla[1]));    
				string   br		= args[3];
				string[] bra	= br.Split(',');
				Point	 pbr	= new Point( int.Parse(bra[0]), int.Parse(bra[1]));    				

				WarpMode mode = WarpMode.Bilinear;
				if( args.Length == 5 && args[4].Length >0 && string.Compare( args[4].Trim(), "Perspective", true) == 0) mode = WarpMode.Perspective;

				return new Warp( ptl, ptr, pbl, pbr, mode);

			}

			return base.ConvertFrom (context, culture, value);
		}


		public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
		{
			if (destinationType == typeof(string)) 
			{
				Warp m = (Warp)value;
				//Warp m = value as Warp;
				//if( m == null) return "null";
				if( m.IsEmpty) return "Empty";

				
				string tl	= string.Format("{0},{1}; ", m.TopLeft.X,m.TopLeft.Y);
				string tr	= string.Format("{0},{1}; ", m.TopRight.X,m.TopRight.Y);
				string bl	= string.Format("{0},{1}; ", m.BottomLeft.X,m.BottomLeft.Y);
				string br	= string.Format("{0},{1}", m.BottomRight.X,m.BottomRight.Y);
				string mode = m.WarpMode == WarpMode.Perspective ? "; Perspective":"";


				return tl + tr + bl + br + mode;
			}

			if (destinationType == typeof(InstanceDescriptor) && value is Warp) 
			{
				Warp m = (Warp)value;
				if( m.WarpMode == WarpMode.Bilinear)
				{
					ConstructorInfo ctor = typeof(Warp).GetConstructor(new Type[] {typeof(Point), typeof(Point), typeof(Point),typeof(Point)});
					if (ctor != null) return new InstanceDescriptor(ctor, new object[] {m.TopLeft,m.TopRight,m.BottomLeft, m.BottomRight});
					else throw base.GetConvertToException( m, typeof(Warp) );
				}
				else
				{
					ConstructorInfo ctor = typeof(Warp).GetConstructor(new Type[] {typeof(Point), typeof(Point), typeof(Point),typeof(Point), typeof( WarpMode)});
					if (ctor != null) return new InstanceDescriptor(ctor, new object[] {m.TopLeft,m.TopRight,m.BottomLeft, m.BottomRight, m.WarpMode});
					else throw base.GetConvertToException( m, typeof(Warp) );
				}
			}

			return base.ConvertTo (context, culture, value, destinationType);
		}

	}

}
